#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Forum

#End Region

Partial Class Usercontrols_Forum_addtopic
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private ForumCheck As New ForumChecks

    Protected Property ForumGuid() As Guid
        Get
            Return GetGuidFromVS("ForumID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ForumID", value)
        End Set
    End Property

    Protected Property TopicGuid() As Guid
        Get
            Return GetGuidFromVS("TopicID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("TopicID", value)
        End Set
    End Property

    Protected Property ForumModerated() As Boolean
        Get
            Return GetBooleanFromVS("ForumModerated")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("ForumModerated", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Topic Message")
        GetQueryStringValues()

        If ForumCheck.IsForumLocked(ForumGuid, Guid.Empty) And (Not IsAdmin()) Then
            Me.uxTopicPanel.Visible = False
            Me.uxMessagesLabel.Text = Resources.Resource.ForumLocked.ToString().Trim()
        Else
            If IsUserAuthenticated() Then

                ConfigBaseEditorCtrls()

                If Me.Page.IsPostBack = False Then
					BuildFCKEditor(EditorPageType.ForumEditor)
                    ForumModerated = ForumCheck.IsForumModerated(ForumGuid, Guid.Empty)
                End If

            End If

            Me.uxTopicPanel.Visible = ShowControls(SecurityMask.Member, True)

        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveTopic()
        Dim Item As Topic = Topic.NewTopic

        Item.TopicName = Me.uxTopicNameTextBox.Text.ToString.Trim
        Item.UserID = CurrentUserID
        Item.ForumID = ForumGuid
		Item.LastUserID = CurrentUserID

		If IsModerator() And ForumModerated Then
			Item.IsApproved = True
		Else
			Item.IsApproved = Not ForumModerated
		End If

        Item.Save()

        TopicGuid = Item.TopicID

    End Sub

    Private Sub SavePost()
        Dim Item As Post = Post.NewPost

		Item.PostBody = Me.uxPostTextbox.Value
        Item.UserID = CurrentUserID
		Item.TopicID = TopicGuid

		If IsModerator() And ForumModerated Then
			Item.IsApproved = True
		Else
			Item.IsApproved = Not ForumModerated
		End If

        Item.Save()

    End Sub

#End Region

#Region " Load Data "

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        SaveTopic()
        SavePost()

		If IsModerator() And ForumModerated Then
			Response.Redirect(String.Format("{0}.aspx?pid=for_post&topicid={1}", AppPage, TopicGuid))
		Else
			If ForumModerated Then
				Response.Redirect(String.Format("{0}.aspx?pid=for_topic&forumid={0}", AppPage, ForumGuid))
			Else
				Response.Redirect(String.Format("{0}.aspx?pid=for_post&topicid={1}", AppPage, TopicGuid))
			End If
		End If

	End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        ForumGuid = GetGuidFromQueryString("forumid")
    End Sub

    Private Sub ConfigBaseEditorCtrls()
		Me.FCKEditor = Me.uxPostTextbox
    End Sub

#End Region

End Class
